Generics עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון

Size: px
Start display at page:

Download "Generics עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון"

Transcription

1 Generics עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון

2 2 Example List myintlist = new LinkedList(); // 1 myintlist.add(new Integer(0)); // 2 Integer x = myintlist.iterator().next(); // 3

3 3 Example List myintlist = new LinkedList(); // 1 myintlist.add(new Integer(0)); // 2 Integer x = (Integer) myintlist.iterator().next(); // 3

4 4 Example List myintlist = new LinkedList(); // 1 myintlist.add(new Integer(0)); // 2 Integer x = (Integer) myintlist.iterator().next(); // 3 The cast on line 3 is annoying. Typically, the programmer knows what kind of data has been placed into a particular list. However, the cast is essential. The compiler can only guarantee that an Object will be returned by the iterator. To ensure the assignment to a variable of type Integer is type safe, the cast is required. Of course, the cast not only introduces clutter. It also introduces the possibility of a run time error. ClassCastException: java.lang.double cannot be cast to java.lang.integer LinkedList myintlist = new LinkedList(); myintlist.add(new Double(0)); Integer x = (Integer) myintlist.iterator().next();

5 5 Example List myintlist = new LinkedList(); // 1 myintlist.add(new Integer(0)); // 2 Integer x = (Integer) myintlist.iterator().next(); // 3 The cast on line 3 is annoying. Typically, the programmer knows what kind of data has been placed into a particular list. However, the cast is essential. The compiler can only guarantee that an Object will be returned by the iterator. To ensure the assignment to a variable of type Integer is type safe, the cast is required. Of course, the cast not only introduces clutter. It also introduces the possibility of a run time error. What if programmers could actually express their intent, and mark a list as being restricted to contain a particular data type? This is the core idea behind generics.

6 6 Solution: Generic Classes Generic (Parametric) classes are classes that have Type Parameters A generic class with a concrete type parameter is an instance of the generic class The type parameter can be the type of variables, parameters or return values.

7 7 Generic Classes in Java Declaring a generic interface or class: interface List<T> { public void add(t t) { public class LinkedList<T> implements List<T> { private T[] _data;...

8 8 Example List myintlist = new LinkedList(); // 1 myintlist.add(new Integer(0)); // 2 Integer x = (Integer) myintlist.iterator().next(); // 3 Solution: USE GENERIC List<Integer> myintlist = new LinkedList<Integer>(); myintlist.add(new Integer(0)); Integer x = myintlist.iterator().next();

9 9 Example List<Integer> myintlist = new LinkedList<Integer>(); // 1 myintlist.add(new Integer(0)); //2 Integer x = myintlist.iterator().next(); // 3 Notice the type declaration for the variable myintlist. It specifies that this is not just an arbitrary List, but a List of Integer, written List<Integer>. We say that List is a generic interface that takes a type parameter - in this case, Integer. We also specify a type parameter when creating the list object. The other thing to pay attention to is that the cast is gone on line 3. Now, you might think that all we ve accomplished is to move the clutter around However, there is a very big difference here. The compiler can now check the type correctness of the program at compile-time. When we say that myintlist is declared with type List<Integer>, this tells us something about the variable myintlist, which holds true wherever and whenever it is used, and the compiler will guarantee it. In contrast, the cast tells us something the programmer thinks is true at a single point in the code.

10 10 Generic Classes in Java (cont d) Creating instances of generics: List<Integer> names = new LinkedList<Integer>(); Generic and Concrete types Generic Type: The parametrized type, e.g List Concrete Type: A generic type with a concrete parameter, e.g List<Integer>

11 11 Defining Simple Generics interface List<T> { public void add(t t) { public class LinkedList<T> implements List<T> { private T[] _data;... This should all be familiar, except for the stuff in angle brackets Type parameters can be used throughout the generic declaration, pretty much where you would use ordinary types

12 12 Defining Simple Generics interface List<T> { public void add(t t) { public class LinkedList<T> implements List<T> { private T[] _data;... In the introduction, we saw invocations of the generic type declaration List, such as List<Integer>. In the invocation (usually called a parameterized type), all occurrences of the formal type parameter (E in this case) are replaced by the actual type You might imagine that List<Integer> stands for a version of List where E has been uniformly replaced by Integer: public interface IntegerList { void add(integer x);

13 13 A helpful, but misleading intuition You might imagine that List<Integer> stands for a version of List where E has been uniformly replaced by Integer It is helpful, because the parameterized type List<Integer> does indeed have methods that look just like this expansion It is misleading, because the declaration of a generic is never actually expanded in this way There aren t multiple copies of the code: not in source, not in binary, not on disk and not in memory This is very different than a C++ template A generic type declaration is compiled once and for all, and turned into a single class file, just like an ordinary class or interface declaration Type parameters are analogous to the ordinary parameters used in methods or constructors Much like a method has formal value parameters that describe the kinds of values it operates on, a generic declaration has formal type parameters When a generic declaration is invoked, the actual type arguments are substituted for the formal type parameters

14 14 Reminder: Sub-Types and Substitution Sub-Typing defines the class relation B is a subtype of A, marked B A. According to the substitution principle, if B A, then an instance of B can substitute an instance of A. Therefore, it is legal to assign an instance of B b to a reference of A a: a b

15 15 Covariant Arrays in Java Covariant: a Cat[] is a Animal[] Early versions of Java and C# did not include generics Making arrays invariant rules out useful polymorphic programs boolean equalarrays (Object[] a1, Object[] a2); void shufflearray(object[] a); Covariant arrays leads to problem with writes into the array

16 16 Covariant Arrays in Java (Cont ) Example: Animal[] an; Cat[] ct = new Cat[30]; an = ct; an[0]= new Animal(); Cat cat = (Cat) an[0]; Runt time error: ArrayStoreException Reading from ct is safely. It is only trying to write to the array that can lead to trouble. Performance problem: each write into an array requires an additional runtime check.

17 17 Example 2 String[] a = new String[1]; Object[] b = a; b[0] = new A(); Runt time error: ArrayStoreException

18 18 Generics and Subtyping Let s test our understanding of generics. Is the following code snippet legal? List<String> ls = new ArrayList<String>(); //1 List<Object> lo = ls; //2

19 19 Generics and Subtyping Let s test our understanding of generics. Is the following code snippet legal? List<String> ls = new ArrayList<String>(); //1 List<Object> lo = ls; //2 Line 1 is certainly legal. The trickier part of the question is line 2. This boils down to the question: is a List of String a List of Object?? Most people s instinct is to answer: sure! Well, take a look at the next few lines: lo.add(new Object()); // 3 String s = ls.get(0); // 4: attempts to assign an Object to a String! The Java compiler will prevent this from happening: line 2 will cause a compile time error

20 20 G<Child> is not a subtype of G<Parent> If Foo is a subclass of Bar, and G is some generic type declaration, it is not the case that G<Foo> is a subtype of G<Bar>

21 21 Wildcards Consider writing a routine that prints out all the elements in a collection: Not using generics void printcollection(collection c) { Iterator i = c.iterator(); for (k = 0; k < c.size(); k++) { System.out.println(i.next()); And here is a naive attempt at writing it using generics: Using generics void printcollection(collection<object> c) { for (Object e : c) { System.out.println(e); The problem is that this new version is much less useful than the old one. Whereas the old code could be called with any kind of collection as a parameter, the new code only takes Collection<Object>, which, as we ve just demonstrated, is not a supertype of all kinds of collections!

22 22 Wildcards? So what is the supertype of all kinds of collections? It s written Collection<?>, that is, a collection whose element type matches anything void printcollection(collection<?> c) { for (Object e : c) { System.out.println(e);? and now, we can call it with any type of collection Notice that inside printcollection(), we can still read elements from c and give them type Object This is always safe, since whatever the actual type of the collection, it does contain objects. Collection<?> c = new ArrayList<String>(); c.add(new Object());

23 23 Wildcards? So what is the supertype of all kinds of collections? It s written Collection<?>, that is, a collection whose element type matches anything void printcollection(collection<?> c) { for (Object e : c) { System.out.println(e);? and now, we can call it with any type of collection Notice that inside printcollection(), we can still read elements from c and give them type Object This is always safe, since whatever the actual type of the collection, it does contain objects. Collection<?> c = new ArrayList<String>(); c.add(new c.add("a"); Object());

24 24 Wildcards? So what is the supertype of all kinds of collections? It s written Collection<?>, that is, a collection whose element type matches anything void printcollection(collection<?> c) { for (Object e : c) { System.out.println(e);? and now, we can call it with any type of collection Notice that inside printcollection(), we can still read elements from c and give them type Object This is always safe, since whatever the actual type of the collection, it does contain objects. It isn t safe to add arbitrary objects to it however: Collection<?> c = new ArrayList<String>(); c.add(new Object()); // compile time error

25 25 Passing data to a wildcard? When the actual type parameter is?, it stands for some unknown type Since we don t know what type that is, we cannot pass anything in The sole exception is null, which is a member of every type On the other hand, given a List<?>, we can call get() and make use of the result The result type is an unknown type, but we always know that it is an object It is therefore safe to assign the result of get() to a variable of type Object or pass it as a parameter where the type Object is expected

26 26 Bounded Wildcards Consider a simple drawing application that can draw shapes such as rectangles and circles: abstract class Shape { abstract void draw(canvas c); class Circle extends Shape { private int x, y, radius; void draw(canvas c) {... class Rectangle extends Shape { private int x, y, width, height; public void draw(canvas c) {... it would be convenient to have a method in Canvas that draws a list of shapes: public void drawall(list<shape> shapes) { for (Shape s : shapes) { s.draw(this); The type rules say that drawall() can only be called on lists of exactly Shape

27 27 Bounded Wildcards What we really want is for the method to accept a list of any kind of shape: public void drawall(list<? extends Shape> shapes) {... There is a small but significant difference here: we have replaced the type List<Shape> with List<? extends Shape> Now drawall() will accept lists of any subclass of Shape, so we can now call it on a List<Circle> if we want List<? extends Shape> is an example of a bounded wildcard. The? Stands for an unknown type, just like the wildcards we saw earlier However, in this case, we know that this unknown type is in fact a subtype of Shape. We say that Shape is the upper bound of the wildcard

28 28 Price of using <? extends Shape> There is, as usual, a price to be paid for the flexibility of using wildcards That price is that it is now illegal to write into shapes in the body of the method For instance, this is not allowed: public void addrectangle(list<? extends Shape> shapes) { shapes.add(0, new Rectangle()); // compile-time error! You should be able to figure out why the code above is disallowed

29 29 Generic Methods Consider writing a method that takes an array of objects and a collection and puts all objects in the array into the collection Here is a first attempt: static void fromarraytocollection(object[] a, Collection<?> c) { for (Object o : a) { c.add(o); // compile time error By now, you will have learned to avoid the beginner s mistake of trying to use Collection<Object> as the type of the collection parameter You may or may not have recognized that using Collection<?> isn t going to work either Recall that you cannot just shove objects into a collection of unknown type

30 30 Generic methods The way to do deal with these problems is to use generic methods Just like class declarations, method declarations can be generic - that is, parameterized by one or more type parameters: static <T> void fromarraytocollection(t[] a, Collection<T> c) { for (T o : a) { c.add(o); // correct

31 31 Type Inference We can call this method with any kind of collection whose element type is a supertype of the element type of the array Object[] oa = new Object[100]; Collection<Object> co = new ArrayList<Object>(); fromarraytocollection(oa, co);// T inferred to be Object Notice that we don t have to pass an actual type argument to a generic method The compiler infers the type argument for us, based on the types of the actual arguments

32 32 Type Inference We can call this method with any kind of collection whose element type is a supertype of the element type of the array Object[] oa = new Object[100]; Collection<Object> co = new ArrayList<Object>(); fromarraytocollection(oa, co);// T inferred to be Object String[] sa = new String[100]; Collection<String> cs = new ArrayList<String>(); fromarraytocollection(sa, cs); fromarraytocollection (java.lang.string[] a, Collection<java.lang.String> c Notice that we don t have to pass an actual type argument to a generic method The compiler infers the type argument for us, based on the types of the actual arguments

33 33 Type Inference We can call this method with any kind of collection whose element type is a supertype of the element type of the array Object[] oa = new Object[100]; Collection<Object> co = new ArrayList<Object>(); fromarraytocollection(oa, co);// T inferred to be Object String[] sa = new String[100]; Collection<String> cs = new ArrayList<String>(); fromarraytocollection(sa, cs);// T inferred to be String fromarraytocollection(sa, co);// Notice that we don t have to pass an actual type argument to a generic method The compiler infers the type argument for us, based on the types of the actual arguments

34 34 Type Inference We can call this method with any kind of collection whose element type is a supertype of the element type of the array Object[] oa = new Object[100]; Collection<Object> co = new ArrayList<Object>(); fromarraytocollection(oa, co);// T inferred to be Object String[] sa = new String[100]; Collection<String> cs = new ArrayList<String>(); fromarraytocollection(sa, cs);// T inferred to be String fromarraytocollection(sa, co);// T inferred to be Object Integer[] ia = new Integer[100]; Float[] fa = new Float[100]; Number[] na = new Number[100]; Collection<Number> cn = new ArrayList<Number>(); fromarraytocollection(ia, cn);// Notice that we don t have to pass an actual type argument to a generic method The compiler infers the type argument for us, based on the types of the actual arguments

35 35 Type Inference We can call this method with any kind of collection whose element type is a supertype of the element type of the array Object[] oa = new Object[100]; Collection<Object> co = new ArrayList<Object>(); fromarraytocollection(oa, co);// T inferred to be Object String[] sa = new String[100]; Collection<String> cs = new ArrayList<String>(); fromarraytocollection(sa, cs);// T inferred to be String fromarraytocollection(sa, co);// T inferred to be Object Integer[] ia = new Integer[100]; Float[] fa = new Float[100]; Number[] na = new Number[100]; Collection<Number> cn = new ArrayList<Number>(); fromarraytocollection(ia, cn);// T inferred to be Number fromarraytocollection(fa, cn);// T inferred to be Number fromarraytocollection(na, cn);// T inferred to be Number fromarraytocollection(na, co);// T inferred to be Object fromarraytocollection(oa,cs); Notice that we don t have to pass an actual type argument to a generic method The compiler infers the type argument for us, based on the types of the actual arguments

36 36 Type Inference We can call this method with any kind of collection whose element type is a supertype of the element type of the array Object[] oa = new Object[100]; Collection<Object> co = new ArrayList<Object>(); fromarraytocollection(oa, co);// T inferred to be Object String[] sa = new String[100]; Collection<String> cs = new ArrayList<String>(); fromarraytocollection(sa, cs);// T inferred to be String fromarraytocollection(sa, co);// T inferred to be Object Integer[] ia = new Integer[100]; Float[] fa = new Float[100]; Number[] na = new Number[100]; Collection<Number> cn = new ArrayList<Number>(); fromarraytocollection(ia, cn);// T inferred to be Number fromarraytocollection(fa, cn);// T inferred to be Number fromarraytocollection(na, cn);// T inferred to be Number fromarraytocollection(na, co);// T inferred to be Object fromarraytocollection(oa,cs);

37 37 Type Inference We can call this method with any kind of collection whose element type is a supertype of the element type of the array Object[] oa = new Object[100]; Collection<Object> co = new ArrayList<Object>(); fromarraytocollection(oa, co);// T inferred to be Object String[] sa = new String[100]; Collection<String> cs = new ArrayList<String>(); fromarraytocollection(sa, cs);// T inferred to be String fromarraytocollection(sa, co);// T inferred to be Object Integer[] ia = new Integer[100]; Float[] fa = new Float[100]; Number[] na = new Number[100]; Collection<Number> cn = new ArrayList<Number>(); fromarraytocollection(ia, cn);// T inferred to be Number fromarraytocollection(fa, cn);// T inferred to be Number fromarraytocollection(na, cn);// T inferred to be Number fromarraytocollection(na, co);// T inferred to be Object fromarraytocollection(oa,cs);//compile-time error fromarraytocollection(na, cs);//

38 38 When should I use generic methods, and when should I use wildcard types? interface Collection<E> { public <T> boolean containsall(collection<t> c); public <T extends E> boolean addall(collection<t> c);?? interface Collection<E> { public boolean containsall(collection<?> c); public boolean addall(collection<? extends E> c); The type parameter T is used only once. The return type doesn t depend on the type parameter, nor does any other argument to the method This tells us that the type argument is being used for polymorphism; its only effect is to allow a variety of actual argument types to be used at different invocation sites. If that is the case, one should use wildcards. Wildcards are designed to support flexible subtyping, which is what we re trying to express here Generic methods allow type parameters to be used to express dependencies among the types of one or more arguments to a method and/or its return type If there isn t such a dependency, a generic method should not be used

39 39 It is possible to use both generic methods and wildcards in tandem class Collections { public static <T> void copy(list<t> dest, List<? extends T> src){... Note the dependency between the types of the two parameters Any object copied from the source list, src, must be assignable to the element type T of the destination list, dst So the element type of src can be any subtype of T - we don t care which The signature of copy expresses the dependency using a type parameter, but uses a wildcard for the element type of the second parameter We could have written the signature for this method another way, without using wildcards at all: class Collections { public static <T, S extends T> void copy(list<t> dest, List<S> src){... This is fine, but while the first type parameter is used both in the type of dst and in the bound of the second type parameter, S, S itself is only used once, in the type of src - nothing else depends on it. This is a sign that we can replace S with a wildcard

40 40 Implementation Generics are implemented by the Java compiler as a front-end conversion called erasure You can (almost) think of it as a source-to-source translation, whereby the generic version is converted to the non-generic version Basically, erasure gets rid of (or erases) all generic type information. All the type information between angle brackets is thrown out, so, for example, a parameterized type like List<String> is converted into List All remaining uses of type variables are replaced by the upper bound of the type variable (usually Object) And, whenever the resulting code isn t type-correct, a cast to the appropriate type is inserted The full details of erasure are beyond our scope, but the simple description we just gave isn t far from the truth

41 41 Type Erasure Generics were introduced to the Java language to provide tighter type checks at compile time and to support generic programming. To implement generics, the Java compiler applies type erasure to: Replace all type parameters in generic types with their bounds or Object if the type parameters are unbounded. The produced bytecode, therefore, contains only ordinary classes, interfaces, and methods. Insert type casts if necessary to preserve type safety. Generate bridge methods to preserve polymorphism in extended generic types. Type erasure ensures that no new classes are created for parameterized types; consequently, generics incur no runtime overhead.

42 42 Erasure and Translation Here, we ve aliased a list of strings and a plain old list: public String loophole(integer x) { List<String> ys = new LinkedList<String>(); List xs = ys; xs.add(x); // compile-time unchecked warning return ys.iterator().next(); We insert an Integer into the list, and attempt to extract a String. This is clearly wrong If we ignore the warning and try to execute this code, it will fail exactly at the point where we try to use the wrong type At run time, this code behaves like: public String loophole(integer x) { List ys = new LinkedList; List xs = ys; xs.add(x); return (String) ys.iterator().next(); // run time error

43 43 Example (2) public <T> T testgeneric(t x){ List<T> ys = new LinkedList<T>(); System.out.println(x); ys.add(x); return ys.iterator().next(); Integer answer = ts.testgeneric(new Integer(1)); String str = ts.testgeneric( Generic"); public Object testgeneric(object x){ List ys = new LinkedList(); System.out.println(x); ys.add(x); return ys.iterator().next(); Integer answer = (Integer) ts.testgeneric(new Integer(1)); String str = (String) ts.testgeneric( Generic");

44 44 A Generic Class is Shared by all its Invocations What does the following code fragment print? List <String> l1 = new ArrayList<String>(); List<Integer> l2 = new ArrayList<Integer>(); System.out.println(l1.getClass() == l2.getclass()); You might be tempted to say false, but you d be wrong. It prints true, because all instances of a generic class have the same run-time class, regardless of their actual type parameters. Indeed, what makes a class generic is the fact that it has the same behavior for all of its possible type parameters; the same class can be viewed as having many different types As consequence, the static variables and methods of a class are also shared among all the instances.

45 45 Casts and InstanceOf Another implication of the fact that a generic class is shared among all its instances, is that it usually makes no sense to ask an instance if it is an instance of a particular invocation of a generic type: Collection cs = new ArrayList<String>(); if (cs instanceof Collection<String>) {... // illegal similarly, Collection<String> cstr = (Collection<String>) cs; // unchecked warning The same is true of type variables: <T> T badcast(t t, Object o) {return (T) o; // unchecked warning Type variables don t exist at run time. This means that they entail no performance overhead in either time nor space, which is nice. Unfortunately, it also means that you can t reliably use them in casts

46 46 More Fun with Wildcards We ve seen examples where bounded wildcards were useful when reading from a data structure Now consider the inverse, a write-only data structure Flush all elements of the collection and return the last element flushed: public static <T> T writeall(collection<t> coll, Sink<T> snk){ T last; for (T t : coll) { last = t; snk.flush(last); return last;... Sink<Object> s; Collection<String> cs; String str = writeall(cs, s); // illegal call

47 47 A solution? As written, the call to writeall() is illegal, as no valid type argument can be inferred; neither String nor Object are appropriate types for T, because the Collection element and the Sink element must be of the same type We can fix this by modifying the signature of writeall() as shown below, using a wildcard public static <T> T writeall(collection<? extends T>, Sink<T>){ String str = writeall(cs, s); // call ok, but wrong return type The call is now legal, but the assignment is erroneous, since the return type inferred is Object because T matches the element type of s, which is Object

48 48 Lower Bounds The solution is to use a form of bounded wildcard we haven t seen yet: wildcards with a lower bound The syntax <? super T> denotes an unknown type that is a supertype of T It is the dual of the bounded wildcards we ve been using, where we use <? extends T> to denote an unknown type that is a subtype of T public static <T> T writeall(collection<t> coll, Sink<? super T> snk){ String str = writeall(cs, s); // Yes! Using this syntax, the call is legal, and the inferred type is String, as desired

49 49 Lower / Upper Bounds = Read / Write permission Upper Bound Marked G<? extends X> A descendant of X Allows to read an instance Lower Bound Marked G<? super Y> An ancestor of Y Allows to write to an instance

Generics עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון

Generics עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון Generics עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון 2 Example List myintlist = new LinkedList(); // 1 myintlist.add(new Integer(0)); // 2 Integer x = (Integer) myintlist.iterator().next(); // 3

More information

Java Generics. Lecture CS1122 Summer 2008

Java Generics.  Lecture CS1122 Summer 2008 Java Generics http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf Lecture 17 -- CS1122 Summer 2008 Java Generics quick review When you see in Java, you are dealing with generics Generics allow

More information

Java Generics Java, summer semester

Java Generics Java, summer semester Java Generics Introduction similar to the templates in C#/C++ but only on first view typed arguments goal clear code type safety Motivational example without generics (

More information

JAVA V Assertions Java, winter semester

JAVA V Assertions Java, winter semester JAVA Assertions 1 Assertion since Java 1.4 the statement with a boolean expression a developer supposes that the expression is always satisfied (evaluates to true) if it is evaluated to false -> error

More information

JAVA V Source files Java, winter semester

JAVA V Source files Java, winter semester JAVA Source files 17.10.2017 1 Unicode programs ~ Unicode comments, identifiers, char and string constants the rest is in ASCII (

More information

Subtyping for behaviour?

Subtyping for behaviour? Subtyping for behaviour? 10/26/10 1 Subtyping for behaviour the inner style class Reservation { date... ; customer... ; void print() { // print Date and Customer inner; class FlightReservation extends

More information

The collections interfaces

The collections interfaces Generics in Java Advanced Programming 4/18/16 1 The collections interfaces Contains unique elements Maps unique keys to values 4/18/16 2 Collections in Java Array has a special language support Iterators

More information

Subtyping for behaviour?

Subtyping for behaviour? Subtyping for behaviour? Reservation date customer print() FlightReservation flight seat print() TrainReservation train waggon seat print() 10/23/2009 1 Subtyping for behaviour the inner style class Reservation

More information

CSE 331 Software Design and Implementation. Lecture 14 Generics 2

CSE 331 Software Design and Implementation. Lecture 14 Generics 2 CSE 331 Software Design and Implementation Lecture 14 Generics 2 Zach Tatlock / Spring 2018 Big picture Last time: Generics intro Subtyping and Generics Using bounds for more flexible subtyping Using wildcards

More information

CSE 331 Software Design and Implementation. Lecture 14 Generics 2

CSE 331 Software Design and Implementation. Lecture 14 Generics 2 CSE 331 Software Design and Implementation Lecture 14 Generics 2 James Wilcox / Winter 2016 Hi, I m James! Big picture Last time: Generics intro Subtyping and Generics Using bounds for more flexible subtyping

More information

Outline. 1 Generic Classes. 2 Generics and Subtyping. Generics. Guy Wiener. Generic Classes. Generics and Subtyping

Outline. 1 Generic Classes. 2 Generics and Subtyping. Generics. Guy Wiener. Generic Classes. Generics and Subtyping Outline 1 2 Outline 1 2 (Parametric) classes are classes that have Parameters A generic class with a concrete type parameter is an instance of the generic class The type parameter can be the type of variables,

More information

Announcements. Lecture 15 Generics 2. Announcements. Big picture. CSE 331 Software Design and Implementation

Announcements. Lecture 15 Generics 2. Announcements. Big picture. CSE 331 Software Design and Implementation CSE 331 Software Design and Implementation Lecture 15 Generics 2 Announcements Leah Perlmutter / Summer 2018 Announcements Quiz 5 is due tomorrow Homework 6 due tomorrow Section tomorrow! Subtyping now

More information

CSE 331 Software Design and Implementation. Lecture 15 Generics 2

CSE 331 Software Design and Implementation. Lecture 15 Generics 2 CSE 331 Software Design and Implementation Lecture 15 Generics 2 Leah Perlmutter / Summer 2018 Announcements Announcements Quiz 5 is due tomorrow Homework 6 due tomorrow Section tomorrow! Subtyping now

More information

#Students per correct answers

#Students per correct answers Generics in Java Programming Projects 14/04/15 1 #Students per correct answers Quiz"2" 10" 9" 8" 7" 6" 5" 4" 3" 2" 1" 0" 10" 9" 8" 7" 6" 5" 4" 3" 2" 1" 0" #"Correct"Answers" 10" 9" 8" 7" 6" 5" 4" 3" 2"

More information

1.1. Annotations History Lesson - C/C++

1.1. Annotations History Lesson - C/C++ 1. Additions Thanks to Dr. James Heliotis. He started it all :) See also here: and see also here: and here: You need to use the tools from the Java release candidate 1 % bash % export PATH=/usr/local/j2sdk1.5.0-rc1/bin:$PATH

More information

11/7/18 JAVA GENERICS. Java Collections. Java Collections. Using Java Collections. Proposals for adding Generics to Java.

11/7/18 JAVA GENERICS. Java Collections. Java Collections. Using Java Collections. Proposals for adding Generics to Java. JAVA GENERICS Lecture CS110 Fall 018 Photo credit: Andrew Kennedy Java Collections Early versions of Java lacked generics interface Collection { /** Return true iff the collection contains ob */ boolean

More information

Photo credit: Andrew Kennedy JAVA GENERICS

Photo credit: Andrew Kennedy JAVA GENERICS Photo credit: Andrew Kennedy JAVA GENERICS Lecture 17 CS2110 Spring 2017 Java Collections 2 Early versions of Java lacked generics interface Collection { /** Return true iff the collection contains ob

More information

Java. Platforms, etc.

Java. Platforms, etc. Java Platforms, etc. Java object oriented (almost) all is object interpreted source code (.java) compiled to the bytecode bytecode (.class) interpreted by the virtual machine platform independent programs

More information

Java. Platforms, etc.

Java. Platforms, etc. Java Platforms, etc. Java object oriented (almost) all is object interpreted source code (.java) compiled to the bytecode bytecode (.class) interpreted by the virtual machine platform independent programs

More information

CSCE 314 Programming Languages

CSCE 314 Programming Languages CSCE 314 Programming Languages! Java Generics II Dr. Hyunyoung Lee!!! 1 Type System and Variance Within the type system of a programming language, variance refers to how subtyping between complex types

More information

PARAMETRIC POLYMORPHISM

PARAMETRIC POLYMORPHISM PARAMETRIC POLYMORPHISM Java C#! Parametric polymorphism: " Java Generics and Generic C# for.net! The idea: the compiler is able to check parametric classes just looking at their definilon 1 Java Generics

More information

Subtyping for behaviour?

Subtyping for behaviour? Subtyping for behaviour? Reservation date customer FlightReservation flight seat print() print() TrainReservation train waggon seat print() 10/4/2004 1 Subtyping for behaviour BETA style class Reservation

More information

Parametric polymorphism and Generics

Parametric polymorphism and Generics Parametric polymorphism and Generics Today s Lecture Outline Parametric polymorphism Java generics Declaring and instantiating generics Bounded types: restricting instantiations Generics and subtyping.

More information

Lecture Outline. Parametric Polymorphism and Java Generics. Polymorphism. Polymorphism

Lecture Outline. Parametric Polymorphism and Java Generics. Polymorphism. Polymorphism Lecture Outline Parametric Polymorphism and Java Generics Parametric polymorphism Java generics Declaring and instantiating generics Bounded types: restricting instantiations Generics and subtyping. Wildcards

More information

CSE 331 Software Design & Implementation

CSE 331 Software Design & Implementation CSE 331 Software Design & Implementation Hal Perkins Autumn 2013 Generics (Polymorphism) (Slides by Mike Ernst and David Notkin) 1 Varieties of abstraction Abstraction over computation: procedures int

More information

CS61B Lecture #25: Java Generics. Last modified: Thu Oct 18 21:04: CS61B: Lecture #25 1

CS61B Lecture #25: Java Generics. Last modified: Thu Oct 18 21:04: CS61B: Lecture #25 1 CS61B Lecture #25: Java Generics Last modified: Thu Oct 18 21:04:53 2018 CS61B: Lecture #25 1 The Old Days Java library types such as List didn t used to be parameterized. All Lists were lists of Objects.

More information

CS61B Lecture #25: Java Generics. Last modified: Thu Oct 19 19:36: CS61B: Lecture #25 1

CS61B Lecture #25: Java Generics. Last modified: Thu Oct 19 19:36: CS61B: Lecture #25 1 CS61B Lecture #25: Java Generics Last modified: Thu Oct 19 19:36:29 2017 CS61B: Lecture #25 1 The Old Days Java library types such as List didn t used to be parameterized. All Lists were lists of Objects.

More information

Generalizing Collection Classes Using Generics with other Java 1.5 Features Integration of Generics with Previous Releases User built generic classes

Generalizing Collection Classes Using Generics with other Java 1.5 Features Integration of Generics with Previous Releases User built generic classes Java Generics 1 Concepts Generalizing Collection Classes Using Generics with other Java 1.5 Features Integration of Generics with Previous Releases User built generic classes 2 What Are Generics? Generics

More information

Software Engineering. Prof. Agostino Poggi

Software Engineering. Prof. Agostino Poggi Agent and Object Technology Lab Dipartimento di Ingegneria dell Informazione Università degli Studi di Parma Software Engineering Generics Prof. Agostino Poggi public class ListOfIntegers { //... fields...

More information

CSE 331. Generics (Parametric Polymorphism)

CSE 331. Generics (Parametric Polymorphism) CSE 331 Generics (Parametric Polymorphism) slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia http://www.cs.washington.edu/331/ 1 Parametric polymorphism

More information

CSE Lecture 7: Polymorphism and generics 16 September Nate Nystrom UTA

CSE Lecture 7: Polymorphism and generics 16 September Nate Nystrom UTA CSE 3302 Lecture 7: Polymorphism and generics 16 September 2010 Nate Nystrom UTA 2 Polymorphism poly = many morph = shape Allow a variable to contain values with different types 3 Subtype polymorphism

More information

Polymorphic (Generic) Programming in Java

Polymorphic (Generic) Programming in Java Polymorphic (Generic) Programming in Java We have used the fact that Java classes are arranged as a tree with the built in class Object at the root to write generic or polymorphic code such as the following

More information

Exercise 8 Parametric polymorphism November 18, 2016

Exercise 8 Parametric polymorphism November 18, 2016 Concepts of Object-Oriented Programming AS 2016 Exercise 8 Parametric polymorphism November 18, 2016 Task 1 Consider the following Scala classes: class A class B extends A class P1[+T] class P2[T

More information

CS108, Stanford Handout #8. Java Generics

CS108, Stanford Handout #8. Java Generics CS108, Stanford Handout #8 Fall, 2007-08 Nick Parlante Java Generics Java generics (added in version 5) are a mixed bag. Some uses of generics are simple to understand and make the code cleaner. They are

More information

Polymorphism. CMSC 330: Organization of Programming Languages. Two Kinds of Polymorphism. Polymorphism Overview. Polymorphism

Polymorphism. CMSC 330: Organization of Programming Languages. Two Kinds of Polymorphism. Polymorphism Overview. Polymorphism CMSC 330: Organization of Programming Languages Polymorphism Polymorphism Definition Feature that allows values of different data types to be handled using a uniform interface Applicable to Functions Ø

More information

INF3110 Programming Languages Object Orientation and Types, part II

INF3110 Programming Languages Object Orientation and Types, part II INF3110 Programming Languages Object Orientation and Types, part II 10/31/17 1 Object Orientation and Types Lecture I Lecture II - Today From predefined (simple) and user-defined (composite) types via

More information

CSC 172 Data Structures and Algorithms. Lecture 3 Spring 2018 TuTh 3:25 pm 4:40 pm

CSC 172 Data Structures and Algorithms. Lecture 3 Spring 2018 TuTh 3:25 pm 4:40 pm CSC 172 Data Structures and Algorithms Lecture 3 Spring 2018 TuTh 3:25 pm 4:40 pm Agenda Administrative aspects Java Generics Chapter 1 ADMINISTRATIVE ASPECTS Workshops Workshops Workshops begin on this

More information

Java Generics -- an introduction. Based on https://docs.oracle.com/javase/tutorial/java/generics/why.html

Java Generics -- an introduction. Based on https://docs.oracle.com/javase/tutorial/java/generics/why.html Java Generics -- an introduction Based on https://docs.oracle.com/javase/tutorial/java/generics/why.html Generics vs. Templates Templates in C++ are compiled into unique code based on the types passed

More information

Polymorphism (generics)

Polymorphism (generics) Polymorphism (generics) CSE 331 University of Washington Michael Ernst Varieties of abstraction Abstraction over computation: procedures (methods) int x1, y1, x2, y2; Math.sqrt(x1*x1 + y1*y1); Math.sqrt(x2*x2

More information

The list abstract data type defined a number of operations that all list-like objects ought to implement:

The list abstract data type defined a number of operations that all list-like objects ought to implement: Chapter 7 Polymorphism Previously, we developed two data structures that implemented the list abstract data type: linked lists and array lists. However, these implementations were unsatisfying along two

More information

CSE 331 Software Design & Implementation

CSE 331 Software Design & Implementation CSE 331 Software Design & Implementation Hal Perkins Winter 2018 Generics UW CSE 331 Winter 2018 1 Varieties of abstraction Abstraction over computation: procedures (methods) int x1, y1, x2, y2; Math.sqrt(x1*x1

More information

A declaration may appear wherever a statement or expression is allowed. Limited scopes enhance readability.

A declaration may appear wherever a statement or expression is allowed. Limited scopes enhance readability. Scope vs. Lifetime It is usually required that the lifetime of a run-time object at least cover the scope of the identifier. That is, whenever you can access an identifier, the run-time object it denotes

More information

Announcements. Java Graphics. Exceptions. Java Odds & Ends

Announcements. Java Graphics. Exceptions. Java Odds & Ends Java Odds & Ends Lecture 25 CS211 Fall 2005 Final Exam Wednesday, 12/14 9:00-11:30am Uris Aud Review Session Sunday, 12/11 1:00-2:30pm Kimball B11 Check your final exam schedule! Announcements For exam

More information

Java: exceptions and genericity

Java: exceptions and genericity Chair of Software Engineering Carlo A. Furia, Marco Piccioni, Bertrand Meyer Java: exceptions and genericity Chair of Software Engineering Carlo A. Furia, Marco Piccioni, Bertrand Meyer Exceptions Exceptions

More information

CS61B Lecture #24. Today: Java support for generic programming. Readings for today: A Java Reference, Chapter 10.

CS61B Lecture #24. Today: Java support for generic programming. Readings for today: A Java Reference, Chapter 10. CS61B Lecture #24 Today: Java support for generic programming Readings for today: A Java Reference, Chapter 10. Readings for Monday: Data Structures, 6.4. Last modified: Fri Oct 19 19:33:03 2012 CS61B:

More information

Advanced Programming - JAVA Lecture 4 OOP Concepts in JAVA PART II

Advanced Programming - JAVA Lecture 4 OOP Concepts in JAVA PART II Advanced Programming - JAVA Lecture 4 OOP Concepts in JAVA PART II Mahmoud El-Gayyar elgayyar@ci.suez.edu.eg Ad hoc-polymorphism Outline Method overloading Sub-type Polymorphism Method overriding Dynamic

More information

More than you ever wanted to know about. Java Generics. Jeff Meister CMSC 420 Summer 2007

More than you ever wanted to know about. Java Generics. Jeff Meister CMSC 420 Summer 2007 More than you ever wanted to know about Java Generics Jeff Meister CMSC 420 Summer 2007 The obligatory review of the boring stuff, or GENERICS: A YOUNG LADY S ILLUSTRATED PRIMER IN FOUR SLIDES Java 1.4:

More information

CSE 331 Software Design & Implementation

CSE 331 Software Design & Implementation CSE 331 Software Design & Implementation Kevin Zatloukal Summer 2017 Generics (Based on slides by Mike Ernst, Dan Grossman, David Notkin, Hal Perkins, Zach Tatlock) Preface This lecture will get into the

More information

Problems. Java Generics. Example. Example. Often you need the same behavior for different kind of classes

Problems. Java Generics. Example. Example. Often you need the same behavior for different kind of classes Problems Often you need the same behavior for different kind of classes Java Generics Use Object references to accommodate any object type Use Generic classes and Method The use of Object references induces

More information

+ Abstract Data Types

+ Abstract Data Types Linked Lists Abstract Data Types An Abstract Data Type (ADT) is: a set of values a set of operations Sounds familiar, right? I gave a similar definition for a data structure. Abstract Data Types Abstract

More information

CS 360: Programming Languages Lecture 12: More Haskell

CS 360: Programming Languages Lecture 12: More Haskell CS 360: Programming Languages Lecture 12: More Haskell Geoffrey Mainland Drexel University Adapted from Brent Yorgey s course Introduction to Haskell. Section 1 Administrivia Administrivia Homework 5 due

More information

INF3110 Programming Languages Object orientation part II

INF3110 Programming Languages Object orientation part II INF3110 Programming Languages Object orientation part II 11/4/16 1 Object Orientation and Types Lecture I Lecture II - Today From predefined (simple) and user-defined (composite) types via Abstract data

More information

CS61B Lecture #23. Today: Java support for generic programming. Readings for today: A Java Reference, Chapter 10.

CS61B Lecture #23. Today: Java support for generic programming. Readings for today: A Java Reference, Chapter 10. CS61B Lecture #23 Announcements: Josh s office hours are now back in his office. HW6 now due Saturday. Partial solar eclipse tomorrow, starting at 1:52PM. Next one in August, 2017. See http://www.timeanddate.com/eclipse/list.html

More information

CSE 331 Software Design and Implementation. Lecture 13 Generics 1

CSE 331 Software Design and Implementation. Lecture 13 Generics 1 CSE 331 Software Design and Implementation Lecture 13 Generics 1 Zach Tatlock / Spring 2018 Varieties of abstraction Abstraction over computation: procedures (methods) int x1, y1, x2, y2; Math.sqrt(x1*x1

More information

Rules and syntax for inheritance. The boring stuff

Rules and syntax for inheritance. The boring stuff Rules and syntax for inheritance The boring stuff The compiler adds a call to super() Unless you explicitly call the constructor of the superclass, using super(), the compiler will add such a call for

More information

Collections, Maps and Generics

Collections, Maps and Generics Collections API Collections, Maps and Generics You've already used ArrayList for exercises from the previous semester, but ArrayList is just one part of much larger Collections API that Java provides.

More information

20 Subclassing and Mutation

20 Subclassing and Mutation Object-Oriented Design Lecture 20 CS 3500 Spring 2010 (Pucella) Tuesday, Mar 30, 2010 20 Subclassing and Mutation Suppose we have a class A that subclasses a class B (which I will write A B). Should we

More information

Properties of an identifier (and the object it represents) may be set at

Properties of an identifier (and the object it represents) may be set at Properties of an identifier (and the object it represents) may be set at Compile-time These are static properties as they do not change during execution. Examples include the type of a variable, the value

More information

CMSC 202. Containers

CMSC 202. Containers CMSC 202 Containers Container Definition A container is a data structure whose purpose is to hold objects. Most languages support several ways to hold objects. Arrays are compiler-supported containers.

More information

Stronger vs Weaker (one more time!) Requires more? Promises more? (stricter specifications on what the effects entail)

Stronger vs Weaker (one more time!) Requires more? Promises more? (stricter specifications on what the effects entail) Final review Stronger vs Weaker (one more time!) Requires more? Promises more? (stricter specifications on what the effects entail) Stronger vs Weaker (one more time!) Requires more? weaker Promises more?

More information

09/02/2013 TYPE CHECKING AND CASTING. Lecture 5 CS2110 Spring 2013

09/02/2013 TYPE CHECKING AND CASTING. Lecture 5 CS2110 Spring 2013 1 TYPE CHECKING AND CASTING Lecture 5 CS2110 Spring 2013 1 Type Checking 2 Java compiler checks to see if your code is legal Today: Explore how this works What is Java doing? Why What will Java do if it

More information

Announcements. Lecture 14 Generics 1. Announcements. CSE 331 Software Design and Implementation. Leah Perlmutter / Summer 2018

Announcements. Lecture 14 Generics 1. Announcements. CSE 331 Software Design and Implementation. Leah Perlmutter / Summer 2018 CSE 331 Software Design and Implementation Lecture 14 Generics 1 Announcements Leah Perlmutter / Summer 2018 Announcements Quiz 5 is due Thursday Homework 6 due Thursday Midterm grades and feedback will

More information

CSE 331 Software Design and Implementation. Lecture 14 Generics 1

CSE 331 Software Design and Implementation. Lecture 14 Generics 1 CSE 331 Software Design and Implementation Lecture 14 Generics 1 Leah Perlmutter / Summer 2018 Announcements Announcements Quiz 5 is due Thursday Homework 6 due Thursday Midterm grades and feedback will

More information

generic programming alberto ferrari university of parma

generic programming alberto ferrari university of parma generic programming alberto ferrari university of parma contents generic programming java generic programming methods & generic programming classes & generic programming java with generics generic methods

More information

Generics method and class definitions which involve type parameters.

Generics method and class definitions which involve type parameters. Contents Topic 07 - Generic Programming I. Introduction Example 1 User defined Generic Method: printtwice(t x) Example 2 User defined Generic Class: Pair Example 3 using java.util.arraylist II. Type

More information

Exam 2. Topics. Preconditions vs. Exceptions. Exam 2 Review. Exam 2 on Thursday, March 29 th. Closed book, closed computer, closed phone

Exam 2. Topics. Preconditions vs. Exceptions. Exam 2 Review. Exam 2 on Thursday, March 29 th. Closed book, closed computer, closed phone Exam 2 Exam 2 on Thursday, March 29 th Exam 2 Review Closed book, closed computer, closed phone You are allowed two cheat pages Either a standard 8.5x11-sized sheet filled on both sides or two standard

More information

301AA - Advanced Programming [AP-2017]

301AA - Advanced Programming [AP-2017] 301AA - Advanced Programming [AP-2017] Lecturer: Andrea Corradini andrea@di.unipi.it Tutor: Lillo GalleBa galleba@di.unipi.it Department of Computer Science, Pisa Academic Year 2017/18 AP-2018-15: Java

More information

Binghamton University. CS-140 Fall Dynamic Types

Binghamton University. CS-140 Fall Dynamic Types Dynamic Types 1 Assignment to a subtype If public Duck extends Bird { Then, you may code:. } Bird bref; Duck quack = new Duck(); bref = quack; A subtype may be assigned where the supertype is expected

More information

Overriding המחלקה למדעי המחשב עזאם מרעי אוניברסיטת בן-גוריון

Overriding המחלקה למדעי המחשב עזאם מרעי אוניברסיטת בן-גוריון Overriding עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון 2 Roadmap A method in a child class overrides a method in the parent class if it has the same name and type signature: Parent void method(int,float)

More information

Generics with Type Bounds

Generics with Type Bounds Generics with Type Bounds Computer Science and Engineering College of Engineering The Ohio State University Lecture 27 Generic Methods Like classes, methods can be generic class ArrayOps { //ordinary nongeneric

More information

Type Checking in COOL (II) Lecture 10

Type Checking in COOL (II) Lecture 10 Type Checking in COOL (II) Lecture 10 1 Lecture Outline Type systems and their expressiveness Type checking with SELF_TYPE in COOL Error recovery in semantic analysis 2 Expressiveness of Static Type Systems

More information

Java Brand Generics. Advanced Topics in Java. Khalid Azim Mughal Version date:

Java Brand Generics. Advanced Topics in Java. Khalid Azim Mughal  Version date: Java Brand Generics Advanced Topics in Java Khalid Azim Mughal khalid@ii.uib.no http://www.ii.uib.no/~khalid/atij/ Version date: 2005-03-03 Khalid Azim Mughal Java Brand Generics 1/40 Overview Introduction

More information

Exercise 8 Parametric polymorphism November 17, 2017

Exercise 8 Parametric polymorphism November 17, 2017 Concepts of Object-Oriented Programming AS 2017 Exercise 8 Parametric polymorphism November 17, 2017 Task 1 Implement a list in Java or C# with two methods: public void add(int i, Object el) public Object

More information

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Weiss Chapter 1 terminology (parenthesized numbers are page numbers) Weiss Chapter 1 terminology (parenthesized numbers are page numbers) assignment operators In Java, used to alter the value of a variable. These operators include =, +=, -=, *=, and /=. (9) autoincrement

More information

CS107 Handout 37 Spring 2007 May 25, 2007 Introduction to Inheritance

CS107 Handout 37 Spring 2007 May 25, 2007 Introduction to Inheritance CS107 Handout 37 Spring 2007 May 25, 2007 Introduction to Inheritance Handout written by Julie Zelenski, updated by Jerry. Inheritance is a language property most gracefully supported by the object-oriented

More information

23. Generics and Adapters

23. Generics and Adapters COMP 401 Prasun Dewan 1 23. Generics and Adapters The term generic implies unspecialized/common behavior. In an object oriented language, it seems to apply to the class Object, which describes the behavior

More information

QUIZ. What is wrong with this code that uses default arguments?

QUIZ. What is wrong with this code that uses default arguments? QUIZ What is wrong with this code that uses default arguments? Solution The value of the default argument should be placed in either declaration or definition, not both! QUIZ What is wrong with this code

More information

8 Understanding Subtyping

8 Understanding Subtyping Object-Oriented Design Lecture 8 CS 3500 Fall 2010 (Pucella) Friday/Tuesday, Oct 8/12, 2010 8 Understanding Subtyping Subtpying is a great way to enable client-side reuse, requiring a client to write a

More information

CMSC 202H. Containers and Iterators

CMSC 202H. Containers and Iterators CMSC 202H Containers and Iterators Container Definition A container is a data structure whose purpose is to hold objects. Most languages support several ways to hold objects Arrays are compiler-supported

More information

Advanced Topics in Java and on Security in Systems Effective Generics. Eirik Eltvik 26 th of February 2007

Advanced Topics in Java and on Security in Systems Effective Generics. Eirik Eltvik 26 th of February 2007 Advanced Topics in Java and on Security in Systems Effective Generics Eirik Eltvik 26 th of February 2007 Taking care when calling Legacy code Checks at compile-time is not always appropriate When? Legacy

More information

301AA - Advanced Programming [AP-2017]

301AA - Advanced Programming [AP-2017] 301AA - Advanced Programming [AP-2017] Lecturer: Andrea Corradini andrea@di.unipi.it Tutor: Lillo GalleBa galleba@di.unipi.it Department of Computer Science, Pisa Academic Year 2017/18 AP-2017-14: Java

More information

Agenda. Objects and classes Encapsulation and information hiding Documentation Packages

Agenda. Objects and classes Encapsulation and information hiding Documentation Packages Preliminaries II 1 Agenda Objects and classes Encapsulation and information hiding Documentation Packages Inheritance Polymorphism Implementation of inheritance in Java Abstract classes Interfaces Generics

More information

DrStrangebrac<E,T> Or how I learned to stop worrying, and love generics...

DrStrangebrac<E,T> Or how I learned to stop worrying, and love generics... DrStrangebrac Or how I learned to stop worrying, and love generics... a presentation to The Seattle Java Users Group by Wilhelm Fizpatrick http://www.agileinformatics.com/ Java is Sun Microsystems,

More information

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur Lecture - 43 Dynamic Binding (Polymorphism): Part III Welcome to Module

More information

Section 10 MATERIAL PULLED FROM LAST SECTION AND LAST YEAR S SLIDES

Section 10 MATERIAL PULLED FROM LAST SECTION AND LAST YEAR S SLIDES Section 10 MATERIAL PULLED FROM LAST SECTION AND LAST YEAR S SLIDES Today s Agenda Administrivia Review Design Patterns Design Pattern Worksheet Course Review Administrivia Friday Demos and Couse Wrap-Up

More information

M301: Software Systems & their Development. Unit 4: Inheritance, Composition and Polymorphism

M301: Software Systems & their Development. Unit 4: Inheritance, Composition and Polymorphism Block 1: Introduction to Java Unit 4: Inheritance, Composition and Polymorphism Aims of the unit: Study and use the Java mechanisms that support reuse, in particular, inheritance and composition; Analyze

More information

Java 5 New Language Features

Java 5 New Language Features Java 5 New Language Features Ing. Jeroen Boydens, BLIN prof dr. ir. Eric Steegmans http://users.khbo.be/peuteman/java5/ Enterprise Programming Last session. Jeroen Boydens 1. JDK v1.4: assert 2. Generics

More information

Software Paradigms (Lesson 3) Object-Oriented Paradigm (2)

Software Paradigms (Lesson 3) Object-Oriented Paradigm (2) Software Paradigms (Lesson 3) Object-Oriented Paradigm (2) Table of Contents 1 Reusing Classes... 2 1.1 Composition... 2 1.2 Inheritance... 4 1.2.1 Extending Classes... 5 1.2.2 Method Overriding... 7 1.2.3

More information

Primitive Java Generic Class

Primitive Java Generic Class Primitive Java Generic Class 1 A buffer pool is a data structure that caches records retrieved from a disk file, in order to improve an application's performance. Typically, the pool stores some sort of

More information

Lecture 13: Subtyping

Lecture 13: Subtyping Lecture 13: Subtyping Polyvios Pratikakis Computer Science Department, University of Crete Type Systems and Programming Languages Pratikakis (CSD) Subtyping CS546, 2018-2019 1 / 15 Subtyping Usually found

More information

Modulo I Java Generics

Modulo I Java Generics Modulo I Java Generics Prof. Ismael H F Santos April 05 Prof. Ismael H. F. Santos - ismael@tecgraf.puc-rio.br 1 Ementa Modulo I - Tópicos em JAVA Generics (incompleto!) April 05 Prof. Ismael H. F. Santos

More information

PIC 20A Collections and Data Structures

PIC 20A Collections and Data Structures PIC 20A Collections and Data Structures Ernest Ryu UCLA Mathematics Last edited: March 14, 2018 Introductory example How do you write a phone book program? Some programmers may yell hash table! and write

More information

Java: introduction to object-oriented features

Java: introduction to object-oriented features Chair of Software Engineering Carlo A. Furia, Marco Piccioni, Bertrand Meyer Java: introduction to object-oriented features Chair of Software Engineering Carlo A. Furia, Marco Piccioni, Bertrand Meyer

More information

Exam Review. CSE 331 Section 10 12/6/12. Slides by Kellen Donohue with material from Mike Ernst

Exam Review. CSE 331 Section 10 12/6/12. Slides by Kellen Donohue with material from Mike Ernst Exam Review CSE 331 Section 10 12/6/12 Slides by Kellen Donohue with material from Mike Ernst Course Logistics All homework s done (except late days) HW8 returned HW7 being graded HW9 will be graded during

More information

CS-202 Introduction to Object Oriented Programming

CS-202 Introduction to Object Oriented Programming CS-202 Introduction to Object Oriented Programming California State University, Los Angeles Computer Science Department Lecture III Inheritance and Polymorphism Introduction to Inheritance Introduction

More information

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance Contents Topic 04 - Inheritance I. Classes, Superclasses, and Subclasses - Inheritance Hierarchies Controlling Access to Members (public, no modifier, private, protected) Calling constructors of superclass

More information

Object-Oriented Design Lecture 11 CS 3500 Spring 2010 (Pucella) Tuesday, Feb 16, 2010

Object-Oriented Design Lecture 11 CS 3500 Spring 2010 (Pucella) Tuesday, Feb 16, 2010 Object-Oriented Design Lecture 11 CS 3500 Spring 2010 (Pucella) Tuesday, Feb 16, 2010 11 Polymorphism The functional iterator interface we have defined last lecture is nice, but it is not very general.

More information

Introducing Generics

Introducing Generics Generics Introducing Generics Generics allow reference types (classes, interfaces, and array types) and methods to be parameterized with type information. An abstract data type (ADT) defines both the types

More information

Java 1.5 in a Nutshell

Java 1.5 in a Nutshell Java 1.5 in a Nutshell Including Generics, Enumerated Types, Autoboxing/Unboxing, and an Enhanced for Loop http://java.sun.com/j2se/1.5.0/docs/guide/language/ CS 2334 University of Oklahoma Brian F. Veale

More information

COMP6700/2140 Generic Methods

COMP6700/2140 Generic Methods COMP6700/2140 Generic Methods Alexei B Khorev and Josh Milthorpe Research School of Computer Science, ANU March 2017 Alexei B Khorev and Josh Milthorpe (RSCS, ANU) COMP6700/2140 Generic Methods March 2017

More information